package com.example.springmvcdemo2.controller;

import com.example.springmvcdemo2.dao.UserDao;
import com.example.springmvcdemo2.pojo.User;
import com.example.springmvcdemo2.respone.JsonResult;
import com.example.springmvcdemo2.respone.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.soap.Addressing;
import java.util.Date;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    UserDao userDao;


    @PostMapping("/reg")
    public JsonResult doReg(@RequestBody User user) {
        user.setCreatedTime(new Date());
        user.setModifiedTime(new Date());
        int row = userDao.insert(user);
        if (row > 0) {
            return JsonResult.ok();
        } else
            return new JsonResult(StatusCode.OPERATION_FAILED);

    }

    @PostMapping("/login/{username}/{password}")
    public JsonResult doLogin(@PathVariable String username, @PathVariable String password) {
        User user = userDao.selectUserByUsername(username);
        if (user==null)
            return new JsonResult(StatusCode.PASSWORD_ERROR);
        else if (user.getPassword().equals(password))
            return new JsonResult(StatusCode.LOGIN_SUCCESS);
        else return new JsonResult(StatusCode.PASSWORD_ERROR);

    }

    @RequestMapping("/test")
    public String test(){
        return "!!!!!";
    }


}
